﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Script.Serialization;
using System.IO;
using System.Net;

namespace AgileRollover.Utility
{
    public class TfsJSON
    {
        private ICredentials credentials;
        private string collectionUri;

        public TfsJSON(string connection)
        {
            credentials = CredentialCache.DefaultCredentials;
            collectionUri = connection;
        }

        public TfsJSON(string connection, string username, string password)
        {
            credentials = new NetworkCredential(username, password);
            collectionUri = connection;
        }

        public Dictionary<string,object> TfsRequest(string reqUri)
        {
            try
            {
                WebRequest request = WebRequest.Create(collectionUri+reqUri);
                request.Credentials = credentials;
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                Stream dataStream = response.GetResponseStream();
                StreamReader dsReader = new StreamReader(dataStream);
                string rawJson = dsReader.ReadToEnd();
                JavaScriptSerializer serializer = new JavaScriptSerializer();
                return (Dictionary<string,object>)serializer.DeserializeObject(rawJson);
            }
            catch (Exception e)
            {
                Console.WriteLine("There was an error in the web request");
                Console.WriteLine(e.Message);
                Environment.Exit(1);
            }
            return null;
        }

        public object TfsTeamSettings(string projectName, string teamName)
        {
            return TfsRequest("/" + projectName + "/" + teamName + "/_api/_wit/teamSettings?__v=1");    
        }

        public List<Project> TfsProjectHierarchy()
        {
            List<Project> list = new List<Project>();

            Dictionary<string, object> tfsRequest = TfsRequest("/_api/_common/GetJumpList?__v=1&navigationContextPackage={}&showStoppedCollections=false");
            if (tfsRequest != null)
            {
                object[] projectList = (object[])tfsRequest.First().Value;
                Dictionary<string, object> projects = (Dictionary<string, object>)projectList.First();
                foreach (Dictionary<string, object> project in (object[])projects["projects"])
                {
                    object[] teams = (object[])project["teams"];
                    Project temp = new Project();
                    temp.name = project["name"].ToString();

                    foreach (object team in teams)
                    {
                        Dictionary<string, object> teaminfo = (Dictionary<string, object>)team;
                        temp.teams.Add(teaminfo["name"].ToString(), teaminfo["tfid"].ToString());
                    }
                    list.Add(temp);
                }
            }
            return list;
        }

        public object TfsCategories(string projectName)
        {
            return TfsRequest("/" + projectName + "/_api/_wit/workItemTypeCategories");
        }

        public object TfsQueryResult( string query)
        {
            return TfsRequest("_api/_wit/query?wiql=" + query);
        }
    }
}
